<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>提取一行作为关联数组</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.pg-fetch-assoc" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">pg_fetch_assoc</h1>
    <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">pg_fetch_assoc</span> &mdash; <span class="dc-title">提取一行作为关联数组</span></p>

   </div>
   <div class="refsect1 unknown-unknown-unknown-seealsr" id="refsect1-function.pg-fetch-assoc-unknown-unknown-unknown-seealsr">
    <h3 class="title">说明</h3>
     <div class="methodsynopsis dc-description">
      <span class="methodname"><strong>pg_fetch_assoc</strong></span>
       ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$result</code></span>
      [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$row</code></span>
     ] ) : <span class="type">array</span></div>

    <p class="para rdfs-comment">
     <span class="function"><strong>pg_fetch_assoc()</strong></span> 和调用 <span class="function"><a href="pg_fetch_array.html" class="function">pg_fetch_array()</a></span>
     加上第三个可选参数 PGSQL_ASSOC 是等价的。它只返回一个关联数组。如果需要数字索引，用
     <span class="function"><a href="pg_fetch_row.html" class="function">pg_fetch_row()</a></span>。
    </p>
    <p class="para">
     <span class="function"><strong>pg_fetch_assoc()</strong></span> 是
     <span class="function"><a href="pg_fetch_row.html" class="function">pg_fetch_row()</a></span>
     的扩展版本。除了将数据存储在数字索引（字段编号）之外，默认还将数组存储在关联索引（字段名）中。
    </p>
    <p class="para">
     <code class="parameter">row</code> 是要被提取的行（记录）编号。第一行为 0。
    </p>
    <p class="para">
     <span class="function"><strong>pg_fetch_assoc()</strong></span> 并不明显比
     <span class="function"><a href="pg_fetch_row.html" class="function">pg_fetch_row()</a></span>
     慢，而且还显著更便于使用。
    </p>
    <p class="para">
     <div class="example" id="example-2490">
      <p><strong>Example #1 <span class="function"><strong>pg_fetch_assoc()</strong></span> 例子</strong></p>
      <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_pconnect</span><span style="color: #007700">(</span><span style="color: #DD0000">"dbname=publisher"</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$conn</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"An&nbsp;error&nbsp;occured.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"SELECT&nbsp;id,&nbsp;author,&nbsp;email&nbsp;FROM&nbsp;authors"</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$result</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"An&nbsp;error&nbsp;occured.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;exit;<br />}<br />while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_fetch_assoc</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'id'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'author'</span><span style="color: #007700">];<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'email'</span><span style="color: #007700">];<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
      </div>

     </div>
    </p>
    <p class="para">
     参见
     <span class="function"><a href="pg_fetch_row.html" class="function">pg_fetch_row()</a></span>，<span class="function"><a href="pg_fetch_array.html" class="function">pg_fetch_array()</a></span>，<span class="function"><a href="pg_fetch_object.html" class="function">pg_fetch_object()</a></span> 和
    <span class="function"><a href="pg_fetch_result.html" class="function">pg_fetch_result()</a></span>。
    </p>
   </div>

  </div></div></div></body></html>